let request = require('../../utils/request.js')

Page({
  data:{
    userID:null,
    showd:false,
    redeem: '',
    exchangelist:{},
    discountslist:[]
  },
  onShow(){
    my.getNetworkType({
      success: (res) => {
        if(!res.networkAvailable||res.networkType=='UNKNOWN'){
          my.showToast({
            content: '网络状态不佳，请检查网络',
            type:'fail',
            duration: 2000
          })
        }
      }
    })
    this.getuserinfo();
  },
  //  弹出框
  activity_click() {
    this.setData({
      showd: !this.data.showd
    })
  },
  //获取inputValue的值
  bindKeyInput(e) {
    this.setData({
      redeem: e.detail.value
    })
  },
  //输入兑换码兑换
  // exchange(){
  //   let url = '/user/'+this.data.userID+'/redeem';
  //   this.data.exchangelist.redeem = this.data.redeem
  //   let success = ()=> {
  //     my.showToast({
  //       type: 'success',
  //       content: '兑换成功',
  //       duration: 3000,
  //     });
  //     my.redirectTo({
  //       url: 'coupon' 
  //     })
  //   }
  //   request.postdata(url, this.data.exchangelist, success, this);
  // },
  //获取用户优惠券信息
  getuserinfo() {
    let success = (res) => {
      // this.data.userID=res.id;
      if (res.discounts) {
        this.data.discountslist = res.discounts
        for (let i = 0; i < this.data.discountslist.length; i++) {
          // if (this.data.discountslist[i].applyType==1){
          if (this.data.discountslist[i].typeId == 0) {
            this.data.discountslist[i].discount = this.data.discountslist[i].value * 10 + '折'
          }
          if (this.data.discountslist[i].typeId == 1) {
            this.data.discountslist[i].discount = this.data.discountslist[i].value + '元'
          }
          // }
          if (this.data.discountslist[i].expireType == 0) {
            this.data.discountslist[i].expireTime = this.data.discountslist[i].endTime
          } else if (this.data.discountslist[i].expireType == 1) {
            this.data.discountslist[i].expireTime = this.data.discountslist[i].recivedTime
          }
        }
        this.setData({
          discountslist: this.data.discountslist
        })
      }
      
    }
    request.getdata("/users/user/show", {}, success, this);
  },
 
})